package com.ldzhao.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ldzhao.blog.aspect.annotation.RedisCache;
import com.ldzhao.blog.dao.beans.AdvertConfig;
import com.ldzhao.blog.dao.mapper.AdvertConfigMapper;
import com.ldzhao.blog.model.vo.AdvertConfigConditionVO;
import com.ldzhao.blog.service.AdvertConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <ClassName>AdvertConfigServiceImpl</ClassName>
 * <Description>广告位配置</Description>
 *
 * @Author liuxianzhao
 * @Date 2018年08月07日 13:41
 */
@Service
public class AdvertConfigServiceImpl implements AdvertConfigService {
    @Autowired
    private AdvertConfigMapper advertConfigMapper;

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<AdvertConfig> findPageBreakByCondition(AdvertConfigConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<AdvertConfig> list = advertConfigMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return new PageInfo<>(list);
    }

    /**
     * 保存一个实体，null的属性不会保存，会使用数据库默认值
     *
     * @param entity
     * @return
     */
    @Override
    @RedisCache(flush = true)
    @Transactional(rollbackFor = Exception.class)
    public AdvertConfig insert(AdvertConfig entity) {
        Assert.notNull(entity, "AdvertConfig不可为空！");
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());
        advertConfigMapper.insertSelective(entity);
        return entity;
    }

    /**
     * 批量插入，支持批量插入的数据库可以使用，例如MySQL,H2等，另外该接口限制实体包含id属性并且必须为自增列
     *
     * @param entities
     */
    @Override
    @RedisCache(flush = true)
    public void insertList(List<AdvertConfig> entities) {
        Assert.notNull(entities, "AdvertConfigs不可为空！");
        List<AdvertConfig> list = new ArrayList<>();
        for (AdvertConfig entity : entities) {
            entity.setUpdateTime(new Date());
            entity.setCreateTime(new Date());
            list.add(entity);
        }
        advertConfigMapper.insertList(list);
    }

    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @RedisCache(flush = true)
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return advertConfigMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    /**
     * 根据主键更新实体全部字段，null值会被更新
     *
     * @param entity
     * @return
     */
    @Override
    @RedisCache(flush = true)
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdvertConfig entity) {
        Assert.notNull(entity, "AdvertConfig不可为空！");
        entity.setUpdateTime(new Date());
        return advertConfigMapper.updateByPrimaryKey(entity) > 0;
    }

    /**
     * 根据主键更新属性不为null的值
     *
     * @param entity
     * @return
     */
    @Override
    @RedisCache(flush = true)
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelective(AdvertConfig entity) {
        Assert.notNull(entity, "AdvertConfig不可为空！");
        entity.setUpdateTime(new Date());
        return advertConfigMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */
    @Override
    public AdvertConfig getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        return advertConfigMapper.selectByPrimaryKey(primaryKey);
    }

    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param entity
     * @return
     */
    @Override
//    @RedisCache
    public AdvertConfig getOneByEntity(AdvertConfig entity) {
        Assert.notNull(entity, "AdvertConfig不可为空！");
        return advertConfigMapper.selectOne(entity);
    }

    /**
     * 查询全部结果，listByEntity(null)方法能达到同样的效果
     *
     * @return
     */
    @Override
    public List<AdvertConfig> listAll() {
        AdvertConfigConditionVO vo = new AdvertConfigConditionVO();
        vo.setPageNumber(1);
        vo.setPageSize(100);
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        return advertConfigMapper.findPageBreakByCondition(null);
    }

    /**
     * 根据实体中的属性值进行查询，查询条件使用等号
     *
     * @param entity
     * @return
     */
    @Override
    public List<AdvertConfig> listByEntity(AdvertConfig entity) {
        Assert.notNull(entity, "AdvertConfig不可为空！");
        return advertConfigMapper.select(entity);
    }

    @Override
//    @RedisCache
    public List<AdvertConfig> queryByPositionKey(String positionKey) {
        AdvertConfigConditionVO vo = new AdvertConfigConditionVO();
        vo.setPositionKey(positionKey);
        return advertConfigMapper.findPageBreakByCondition(vo);
    }
}
